package com.viewpagertext.help;


import com.viewpagertext.json.ListFragmentSongMusics;
import com.viewpagertext.utils.HttpUtil;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.io.IOException;

import okhttp3.Call;
import okhttp3.Response;

/**
 * @author ironjam
 * @version 1.0
 * @project AndroidMusic-master
 * @description 用于获取免费音乐的API
 * @date 2024/5/26 18:08:05
 */
public class GetMusicHelp {

    private String FirstPath1 = "https://www.hhlqilongzhu.cn/api/dg_BDbdmusic.php?gm=";
    private String FirstPath2 = "&n=";
    private String FirstPath3 = "&type=json";

    private String SecondPath1 = "https://zj.v.api.aa1.cn/api/qqmusic/demo.php?type=1&q=";
    private String SecondPath2 = "&p=1&n=1";
    private String LastPath1 =
            "http://music.163.com/api/search/get/web?csrf_token=hlpretag=&hlposttag=&s=";
    private String LastPath2 = "&type=1&offset=0&total=true&limit=2";
    private String ThridPath1 = "http://ovoa.cc/api/QQmusic.php?msg=";
    private String ThridPath2 = "&n=";
    private String ThridPath3 = "&type=json";


    public ListFragmentSongMusics.DataBean getOneSong(String songName) {
        ListFragmentSongMusics.DataBean song = new ListFragmentSongMusics.DataBean();
        //获取一首歌曲
        if (doInFirst(song, songName)) return song;
        if (doInSecond(song, songName)) return song;
        if (doInThrid(song, songName)) return song;
        return null;
    }

    private boolean doInThrid(ListFragmentSongMusics.DataBean song, String songName) {
        final boolean[] flag = {false};
        HttpUtil.sendOkHttpRequest(ThridPath1 + songName + ThridPath2 + "1" + ThridPath3, new okhttp3.Callback() {
            @Override
            public void onFailure(Call call, IOException e) {

                // Toast.makeText(getActivity(),"IO发生异常",Toast.LENGTH_SHORT).show();
            }

            @Override
            public void onResponse(Call call, Response response) throws IOException {
                String ListResponseData = response.body().string();//得到服务器返回的具体内容
                JSONObject jsonObject = null;
                try {

                    jsonObject = new JSONObject(ListResponseData);
                    int code = jsonObject.getInt("code");
                    if (code == 200) {
                        JSONObject dataObject = jsonObject.getJSONObject("data");
                        String url = dataObject.getString("src");
                        song.setUrl(url);
                        String pic = dataObject.getString("cover");
                        song.setPic(pic);
                        String name = dataObject.getString("songname");
                        song.setName(name);
                        String singer = dataObject.getString("name");
                        song.setSinger(singer);
                        flag[0] = true;
                    }
                } catch (JSONException e) {
                    throw new RuntimeException(e);
                }

            }
        });
        return flag[0];

    }

    private boolean doInSecond(ListFragmentSongMusics.DataBean song, String songName) {
        final boolean[] flag = {false};
        HttpUtil.sendOkHttpRequest(SecondPath1 + songName + SecondPath2, new okhttp3.Callback() {
            @Override
            public void onFailure(Call call, IOException e) {

                e.printStackTrace();
//                        Toast.makeText(getActivity(),"IO发生异常",Toast.LENGTH_SHORT).show();
            }

            @Override
            public void onResponse(Call call, Response response1) throws IOException {

                String ListResponseData = response1.body().string();//得到服务器返回的具体内容

                if (!ListResponseData.startsWith("<html>")) {

                    JSONObject jsonObject = null;
                    try {
                        jsonObject = new JSONObject(ListResponseData);
                        int code = jsonObject.getInt("code");

                        if (code == 200) {
                            JSONArray listArray = jsonObject.getJSONArray("list");
                            JSONObject listObject = listArray.getJSONObject(0);
                            String url = listObject.getString("url");
                            song.setUrl(url);
                            flag[0] = true;
                        }
                    } catch (JSONException e) {
                        throw new RuntimeException(e);
                    }

                }


            }
        });
        return flag[0];
    }

    private boolean doInFirst(ListFragmentSongMusics.DataBean song, String songName) {
        final boolean[] flag = {false};
        HttpUtil.sendOkHttpRequest(FirstPath1 + songName + FirstPath2 + "1" + FirstPath3, new okhttp3.Callback() {
            @Override
            public void onFailure(Call call, IOException e) {

                // Toast.makeText(getActivity(),"IO发生异常",Toast.LENGTH_SHORT).show();
            }

            @Override
            public void onResponse(Call call, Response response) throws IOException {
                String ListResponseData = response.body().string();//得到服务器返回的具体内容
                JSONObject jsonObject = null;
                try {

                    jsonObject = new JSONObject(ListResponseData);
                    int code = jsonObject.getInt("code");
                    if (code == 200) {
                        String url = jsonObject.getString("music_url");
                        song.setUrl(url);
                        String pic = jsonObject.getString("cover");
                        song.setPic(pic);
                        String name = jsonObject.getString("title");
                        song.setName(name);
                        String singer = jsonObject.getString("singer");
                        song.setSinger(singer);
                        flag[0] = true;
                    }
                } catch (JSONException e) {
                    throw new RuntimeException(e);
                }

            }
        });
        return flag[0];

    }


}
